#!/usr/bin/env python
# -*- coding:utf-8 -*-
from pyquery import PyQuery

from base.gsxt_base_worker import GsxtBaseWorker


class GsxtAllWorker(GsxtBaseWorker):
    def __init__(self, **kwargs):
        GsxtBaseWorker.__init__(self, **kwargs)

    def get_search_result_html(self, keyword, session):
        param_list = []
        try:
            url = 'http://{host}/index.html'.format(host=self.host)
            content = self.get_captcha_geetest(url, '#keyword', '#btn_query', keyword, 'div.search_result',
                                               proxy_type=2)
            if content is None:
                return param_list, self.SEARCH_ERROR

            jq = PyQuery(content, parser='html')
            item_list = jq.find('a.search_list_item.db').items()
            for item in item_list:
                href = item.attr('href')
                if href is None or href == '':
                    continue
                url = 'http://{host}{href}'.format(host=self.host, href=href)
                param_list.append({'url': url})
        except Exception as e:
            self.log.exception(e)
            return param_list, self.SEARCH_ERROR

        return param_list, self.SEARCH_SUCCESS if len(param_list) > 0 else self.SEARCH_NOTHING_FIND

    def get_detail_html_list(self, seed, session, detail_list):
        data_list = []
        rank = 0
        for detail_param in detail_list:
            try:
                pass
                # data_list.append(data)
            except Exception as e:
                self.log.exception(e)

        self.target_db.insert_batch_data(self.target_table, data_list)
        return len(data_list)
